/*
 * Created by JFormDesigner on Thu Jun 23 18:42:58 CST 2022
 */

package jw3.c10.view;

import java.awt.event.*;

import jw3.c10.utils.DBConnection;

import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class Consume extends JFrame {
    //积分
    public static int JNum;

    public Consume() {
        initComponents();
        this.setTitle("消费记录查询(管理员)");
        this.setSize(800,600);
        this.setVisible(true);
    }

    Vector<Vector<Object>> data;

    private void showData( String name) {

        String sql="SELECT a.ConsumptionID,b.UserName,a.Details,a.Cprice,a.ConsumptionDate,a.CreateoutDate FROM consumption a JOIN userinfo b ON a.UserID=b.UserID where UserName like?";

        try {
            Connection conn=DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,"%"+name+"%");
            ResultSet rs=ps.executeQuery();
            data =new Vector<Vector<Object>>();
            while (rs.next()){
                Vector<Object> data1=new Vector<Object>();
                data1.add(rs.getInt("ConsumptionID"));
                data1.add(rs.getString("UserName"));
                data1.add(rs.getString("Details"));
                data1.add(rs.getDouble("Cprice"));
                data1.add(rs.getString("ConsumptionDate"));
                data1.add(rs.getString("CreateoutDate"));
                data.add(data1);

            }
            DBConnection.close(rs,ps,conn);
            Vector<String> heads=new Vector<String>();
            heads.add("消费单编号");
            heads.add("用户");
            heads.add("商品");
            heads.add("价格");
            heads.add("消费时间");
            heads.add("制表时间");


            DefaultTableModel dtm=new DefaultTableModel(data,heads);
            table1.setModel(dtm);



        } catch (Exception e) {
            e.printStackTrace();
        }
    }


private void button1MouseClicked(MouseEvent e) {
    // TODO add your code here
    // TODO add your code here

    showData(textField1.getText());



}

private void button2MouseClicked(MouseEvent e) {
    // TODO add your code here
    dialog1.setModal(true);
    dialog1.setVisible(true);
}

private void button3MouseClicked(MouseEvent e) {
    // TODO add your code here
    String Aoumnt=textField2.getText();

    int a=Integer.parseInt(textField3.getText());
    JNum=a*2;
    if (a>0){
        JOptionPane.showMessageDialog(null,"充值成功！");
        label4.setText(""+a);
        label6.setText(""+JNum);

    }else {
        JOptionPane.showMessageDialog(null,"请输入正确金额！");
        textField3.setText("");
    }

}

private void button5MouseClicked(MouseEvent e) {
    // TODO add your code here
    new Jindex();
}

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        button1 = new JButton();
        textField1 = new JTextField();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button2 = new JButton();
        label2 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        button5 = new JButton();
        dialog1 = new JDialog();
        label1 = new JLabel();
        textField2 = new JTextField();
        label3 = new JLabel();
        textField3 = new JTextField();
        button3 = new JButton();
        button4 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- button1 ----
        button1.setText("\u67e5\u8be2");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(405, 60, 85, button1.getPreferredSize().height);
        contentPane.add(textField1);
        textField1.setBounds(180, 60, 164, 35);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(600, 300));
            panel1.setOpaque(false);
            panel1.setLayout(new GridLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(110, 190), panel1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u5145\u503c");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(555, 60), button2.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u4f59\u989d\uff1a");
        contentPane.add(label2);
        label2.setBounds(110, 145, 55, label2.getPreferredSize().height);

        //---- label4 ----
        label4.setText("0");
        contentPane.add(label4);
        label4.setBounds(175, 145, 50, label4.getPreferredSize().height);

        //---- label5 ----
        label5.setText("\u79ef\u5206\uff1a");
        contentPane.add(label5);
        label5.setBounds(245, 145, 60, label5.getPreferredSize().height);

        //---- label6 ----
        label6.setText("0");
        contentPane.add(label6);
        label6.setBounds(315, 145, 50, label6.getPreferredSize().height);

        //---- button5 ----
        button5.setText("\u79ef\u5206\u5151\u6362");
        button5.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button5MouseClicked(e);
            }
        });
        contentPane.add(button5);
        button5.setBounds(new Rectangle(new Point(395, 140), button5.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);

            //---- label1 ----
            label1.setText("\u8d26\u53f7:");
            dialog1ContentPane.add(label1);
            label1.setBounds(5, 85, 80, label1.getPreferredSize().height);
            dialog1ContentPane.add(textField2);
            textField2.setBounds(105, 80, 140, textField2.getPreferredSize().height);

            //---- label3 ----
            label3.setText("\u5145\u503c\u91d1\u989d:");
            dialog1ContentPane.add(label3);
            label3.setBounds(5, 140, 70, label3.getPreferredSize().height);
            dialog1ContentPane.add(textField3);
            textField3.setBounds(105, 130, 140, textField3.getPreferredSize().height);

            //---- button3 ----
            button3.setText("\u786e\u8ba4");
            button3.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button3MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button3);
            button3.setBounds(new Rectangle(new Point(25, 225), button3.getPreferredSize()));

            //---- button4 ----
            button4.setText("\u53d6\u6d88");
            dialog1ContentPane.add(button4);
            button4.setBounds(new Rectangle(new Point(155, 225), button4.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

    }



    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JButton button1;
    private JTextField textField1;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button2;
    private JLabel label2;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JButton button5;
    private JDialog dialog1;
    private JLabel label1;
    private JTextField textField2;
    private JLabel label3;
    private JTextField textField3;
    private JButton button3;
    private JButton button4;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

}
